﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using moose.irc;

namespace mooseirc.client.windowsforms {

    /// <summary>
    /// Collects Communication Channels
    /// </summary>
    [CLSCompliant(true)]
    public class CommunicationChannelList {

        #region [Local Attribtues]

        /// <summary>
        /// Collects our channels
        /// </summary>
        private Dictionary<IrcEntity, CommunicationChannelForm> m_channels;

        #endregion

        #region [Construction]

        /// <summary>
        /// Initialize Communication Channel List
        /// </summary>
        public CommunicationChannelList()
            : base() {
            m_channels = new Dictionary<IrcEntity, CommunicationChannelForm>();
            return;
        }

        #endregion

        #region [Public Methods]

        /// <summary>
        /// Get or create and get communication form 
        /// </summary>
        /// <param name="entity">entity we want to communicate with</param>
        /// <returns>reference to the (possibly new) commujnication channel form</returns>
        public CommunicationChannelForm get(IrcSession session, MainForm parent_form, IrcEntity entity) {

            if (m_channels.ContainsKey(entity))
                return m_channels[entity];
            CommunicationChannelForm channel_form;
            if (entity.GetType() == typeof(IrcUser)) {
                channel_form = new QueryForm(session, parent_form, (IrcUser)entity);
                channel_form.Show(); //this is needed for query dialogs, since channels have their own create events...
            } else
                channel_form = new ChannelForm(session, parent_form, (IrcChannel)entity);
            m_channels[entity] = channel_form;
            return m_channels[entity];
        }

        /// <summary>
        /// Get all Forms
        /// </summary>
        /// <returns></returns>
        public IEnumerable<CommunicationChannelForm> get_all() {
            return m_channels.Values;
        }

        /// <summary>
        /// Remove channel from from collection
        /// </summary>
        /// <param name="entity"></param>
        public void remove(IrcEntity entity) {
            if (m_channels.ContainsKey(entity))
                m_channels.Remove(entity);
        }

        #endregion

    }
}
